﻿using System;
using System.Windows.Forms;
using mshtml;

namespace Z.GenerateIconProject.WinForm.UI
{
    public partial class ProjectSetting

    {
        public ProjectSetting(WebBrowser browser)
        {
            HtmlDocument doc = browser.Document;

            try
            {
                uiProjectNameLabel = (IHTMLElement) doc.GetElementById("uiProjectNameLabel").DomElement;
                uiProjectName = (HTMLInputElement) doc.GetElementById("uiProjectName").DomElement;
                uiProjectNamespaceLabel = (IHTMLElement) doc.GetElementById("uiProjectNamespaceLabel").DomElement;
                uiProjectNamespace = (HTMLInputElement) doc.GetElementById("uiProjectNamespace").DomElement;
                uiFrameworkNet35Option = (HTMLInputElement) doc.GetElementById("uiFrameworkNet35Option").DomElement;
                uiFrameworkNet20Option = (HTMLInputElement) doc.GetElementById("uiFrameworkNet20Option").DomElement;
                uiFileHeaderText = (HTMLTextAreaElement) doc.GetElementById("uiFileHeaderText").DomElement;
                    
                uiError = (IHTMLElement) doc.GetElementById("uiError").DomElement;
                uiErrorMandatoryProjectName = (IHTMLElement) doc.GetElementById("uiErrorMandatoryProjectName").DomElement;
                uiErrorMandatoryProjectNamespace = (IHTMLElement) doc.GetElementById("uiErrorMandatoryProjectNamespace").DomElement;
                uiErrorInvalidProjectName = (IHTMLElement) doc.GetElementById("uiErrorInvalidProjectName").DomElement;
                uiErrorInvalidProjectNamespace = (IHTMLElement) doc.GetElementById("uiErrorInvalidProjectNamespace").DomElement;

                uiErrorStep2 = (IHTMLElement) doc.GetElementById("uiErrorStep2").DomElement;
                uiErrorMandatoryIconPrefix = (IHTMLElement) doc.GetElementById("uiErrorMandatoryIconPrefix").DomElement;
                uiErrorMandatoryIconPath = (IHTMLElement) doc.GetElementById("uiErrorMandatoryIconPath").DomElement;
                uiErrorInvalidIconPrefix = (IHTMLElement) doc.GetElementById("uiErrorInvalidIconPrefix").DomElement;
                uiErrorInvalidIconPath = (IHTMLElement) doc.GetElementById("uiErrorInvalidIconPath").DomElement;
                uiIconPrefixLabel = (IHTMLElement) doc.GetElementById("uiIconPrefixLabel").DomElement;
                uiIconPrefix = (HTMLInputElement) doc.GetElementById("uiIconPrefix").DomElement;
                uiIconPathLabel = (IHTMLElement) doc.GetElementById("uiIconPathLabel").DomElement;
                uiIconPath = (HTMLInputElement) doc.GetElementById("uiIconPath").DomElement;
                uiIconSetsTable = (HTMLTable) doc.GetElementById("uiIconSetsTable").DomElement;

                var uiAddIconSets = (HTMLButtonElement) doc.GetElementById("uiAddIconSets").DomElement;
                var uiProjectGenerate = (HTMLButtonElement) doc.GetElementById("uiProjectGenerate").DomElement;

                ((HTMLButtonElementEvents2_Event) uiAddIconSets).onclick += AddIconSets_OnClick;
                ((HTMLButtonElementEvents2_Event) uiProjectGenerate).onclick += ProjectGenerate_OnClick;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"General Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Execute()
        {
            try
            {
                ResetErrorCss();
                TrimValue();

                if (IsValid())
                {
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"General Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void ResetErrorCss()
        {
            uiError.className = uiError.className.Replace(" error", " valid");

            uiProjectNameLabel.className = uiProjectNameLabel.className.Replace(" error", " valid");
            uiProjectNamespaceLabel.className = uiProjectNamespaceLabel.className.Replace(" error", " valid");
            uiErrorMandatoryProjectName.className = uiErrorMandatoryProjectName.className.Replace(" error", " valid");
            uiErrorMandatoryProjectNamespace.className = uiErrorMandatoryProjectNamespace.className.Replace(" error", " valid");
            uiErrorInvalidProjectName.className = uiErrorInvalidProjectName.className.Replace(" error", " valid");
            uiErrorInvalidProjectNamespace.className = uiErrorInvalidProjectNamespace.className.Replace(" error", " valid");
        }

        public void TrimValue()
        {
            if (!string.IsNullOrEmpty(uiProjectName.value))
            {
                uiProjectName.value = uiProjectName.value.Trim();
            }
            if (!string.IsNullOrEmpty(uiProjectNamespace.value))
            {
                uiProjectNamespace.value = uiProjectNamespace.value.Trim();
            }
        }

        public bool AddIconSets_OnClick(IHTMLEventObj pEvtObj)
        {
            var row = (HTMLTableRow) uiIconSetsTable.insertRow();
            var cell1 = (HTMLTableCell) row.insertCell();
            var cell2 = (HTMLTableCell) row.insertCell();
            var cell3 = (HTMLTableCell) row.insertCell();

            cell1.innerText = uiIconPrefix.value;
            cell2.innerText = uiIconPath.value;
            cell3.innerText = "toto3";
            return false;
        }

        public bool ProjectGenerate_OnClick(IHTMLEventObj pEvtObj)
        {
            HtmlDocument doc = WebBrowser.Document;

            var uiProjectName = (HTMLInputElement) doc.GetElementById("uiProjectName").DomElement;
            var uiProjectNamespace = (HTMLInputElement) doc.GetElementById("uiProjectNamespace").DomElement;

            var page = new ProjectSetting(WebBrowser);
            page.Execute();
            return false;
        }

        public bool IsValid()
        {
            bool isValid = true;

            if (string.IsNullOrEmpty(uiProjectName.value))
            {
                uiProjectNameLabel.className = uiProjectNameLabel.className.Replace(" valid", " error");
                uiErrorMandatoryProjectName.className = uiErrorMandatoryProjectName.className.Replace(" valid", " error");
                isValid = false;
            }
            else if (uiProjectName.value.Contains(" "))
            {
                uiProjectNameLabel.className = uiProjectNameLabel.className.Replace(" valid", " error");
                uiErrorInvalidProjectName.className = uiErrorInvalidProjectName.className.Replace(" valid", " error");
                isValid = false;
            }

            if (string.IsNullOrEmpty(uiProjectNamespace.value))
            {
                uiProjectNamespaceLabel.className = uiProjectNamespaceLabel.className.Replace(" valid", " error");
                uiErrorMandatoryProjectNamespace.className = uiErrorMandatoryProjectNamespace.className.Replace(" valid", " error");
                isValid = false;
            }
            else if (uiProjectNamespace.value.Contains(" "))
            {
                uiProjectNamespaceLabel.className = uiProjectNamespaceLabel.className.Replace(" valid", " error");
                uiErrorInvalidProjectNamespace.className = uiErrorInvalidProjectNamespace.className.Replace(" valid", " error");
                isValid = false;
            }


            if (!isValid)
            {
                uiError.className = uiError.className.Replace(" valid", " error");
            }

            return isValid;
        }
    }
}